package cn.mothin.frame.help;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.dispatcher.Dispatcher;
import org.springframework.stereotype.Service;

import cn.mothin.frame.core.exception.ErrorUrlException;

import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.RuntimeConfiguration;
import com.opensymphony.xwork2.config.entities.ActionConfig;

@Service
public class ActionScanner {
private Map<String, String> baseUrlMap = null;
	
	private void prepare() {
		baseUrlMap = new HashMap<String, String>();

		Dispatcher dispatcher = Dispatcher.getInstance();
		ConfigurationManager configurationManager = dispatcher
				.getConfigurationManager();
		Configuration configuration = configurationManager.getConfiguration();
		RuntimeConfiguration runtimeConfiguration = configuration
				.getRuntimeConfiguration();
		Map<String, Map<String, ActionConfig>> maps = runtimeConfiguration
				.getActionConfigs();

		for (String k1 : maps.keySet()) {
			Map<String, ActionConfig> map = maps.get(k1);
			for (String k2 : map.keySet()) {
				ActionConfig config = map.get(k2);
				String className = config.getClassName();
				String nameSpace = (config.getPackageName().split("#", 3)[2]);
				String name = config.getName();
				baseUrlMap.put(className,
						(nameSpace.length() > 0 ? nameSpace.substring(1) + "/"
								: nameSpace) + name);
			}
		}
	}

	/**
	 * 计算Action地址
	 * 
	 * @param className
	 *            类名
	 * @param methodName
	 *            方法名
	 * @return
	 */
	public String getAction(String className, String methodName) {
		if(baseUrlMap==null){
			prepare();
		}
		if (baseUrlMap.containsKey(className)) {
			if (methodName == null || methodName.length() == 0
					|| methodName.equals("execute")) {
				return baseUrlMap.get(className) + ".action";
			}
			return baseUrlMap.get(className) + "!" + methodName + ".action";
		}
		throw new ErrorUrlException();
	}

	/**
	 * 计算Action地址
	 * 
	 * @param clazz
	 *            类
	 * @param methodName
	 *            方法名
	 * @return
	 */
	public String getAction(Class<?> clazz, String methodName) {
		return getAction(clazz.getName(), methodName);
	}

	public List<String> getActionClassNameList() {
		if(baseUrlMap==null){
			prepare();
		}
		
		List<String> list = new ArrayList<String>();
		for (String key : baseUrlMap.keySet()) {
			list.add(key);
		}
		return list;
	}

	public String getClassName(String action) {
		if(baseUrlMap==null){
			prepare();
		}
		
		String target = null;
		if (action.contains("!")) {
			String[] ss = action.split("!");
			target = ss[0];
		} else {
			String s=action;
			if(action.contains("?")){
				String[] ss=action.split("\\?");
				s=ss[0];
			}
			target = s.replace(".action", "");
		}
		for (Map.Entry<String, String> entry : baseUrlMap.entrySet()) {
			if (entry.getValue().equals(target)) {
				return entry.getKey();
			}
		}
		return null;
	}

	public String getMethodName(String action) {
		if (action.contains("!")) {
			String[] ss = action.split("!");
			String s= ss[1].replace(".action", "");
			if(s.contains("?")){
				String[] sss=s.split("\\?");
				return sss[0];
			}
			return s;
			
		}
		return "execute";
	}
}
